/*
 * CatPred.java
 *
 * Created on den 2 maj 2007, 09:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package grex.Nodes.Depricated;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import grex.Options;
import grex.Data.Prediction;
import grex.Data.PredictionContainer;
import grex.Nodes.GeneException;
import grex.Nodes.ICategoricalOperator;
import grex.Nodes.Node;

import java.text.NumberFormat;

public class ProbVal extends Node {

    private ProbVar var;
    private static final int NROFCILDREN = 0;
    private double internalValue = -1;
    private double valValue = -1;
    private String realValue = "Not initialized";


    public ProbVal() {
        super(NROFCILDREN);
        valValue = Options.rnd.nextDouble();
    }


    public synchronized void execute(Node parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;
        if (mode == Node.TRAIN_MODE) {

            Node[] childs = parent.getChildren();
            var = (ProbVar) (childs[0]==this?childs[1]:childs[0]);
            internalValue = environment.getInternalNumericalValue(var.getVarNr(), valValue,getFold());
         //   internalValue = environment.getInternalNumericalValue(var.getVarNr(), valValue);
        }

        for (Prediction p : data.values()) {
            p.setPrediction(internalValue);
        }
    }

    public double getInternalValue() {
        return internalValue;
    }

    public void setValValue(double value){
        valValue = value;
    }

    public void setRealValue(String realValue){
        this.realValue=realValue;
    }

    public Object clone() {
        ProbVal val = new ProbVal();
        val.setFold(getFold());
        val.internalValue= internalValue;
        val.valValue=valValue;
        val.var=var;
        val.setOptions(ops);
        val.setEnvironment(environment);
        val.setRealValue(realValue);
        val.setParent(parent);
        return val;
    }

    public String toString() {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(3);
        if(parent!=null){
            if(parent instanceof ICategoricalOperator)
                realValue = environment.getRealClassValue(var.getVarNr(), internalValue);
            else
                realValue = nf.format(internalValue);
        }
        return realValue;
    }


}

